<template>
  <div>
    <el-dialog v-dialogDrag title="编辑委托合同" :visible="editShow" width="40%" @close="dialogClose"
               :close-on-click-modal="false">
      <!-- <div class="buttdiv">
          <a :href="contraction" @click="contractiondown">合同下载</a>
          <a :href="contraction" target="_blank" @click="contractionView">合同预览</a>
      </div> -->

      <el-form ref="editForm" :model="formFileds" :rules="rules" label-width="auto">
        <el-form-item label="合同编号" prop="wthtbh">
          <el-input v-model="formFileds.wthtbh" disabled></el-input>
        </el-form-item>
        <el-form-item label="委托方类型" prop="wtflx">
          <el-select v-model="formFileds.wtflx" @change="update(formFileds.wtflx)">
              <el-option label="个人" value="个人"></el-option>
              <el-option label="企业机构" value="企业机构"></el-option>
              <el-option label="司法机构" value="司法机构"></el-option>
          </el-select>
<!--          <el-input v-model="formFileds.wtflx" disabled></el-input>-->
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx != '司法机构'" label="委托合同类型" prop="wthtlx">
          <el-select v-model="formFileds.wthtlx">
              <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
          </el-select>
<!--          <el-input v-model="formFileds.wthtlx" disabled></el-input>-->
        </el-form-item>
        <el-form-item label="业务类型：" prop="ywlx">
          <el-select v-model="formFileds.ywlx" clearable placeholder="请选择业务类型" @change="getpgmd">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
          </el-select>
<!--          <el-input v-model="formFileds.ywlx" disabled></el-input>-->
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="司法委托编号" prop="sfwtbh">
          <el-input v-model="formFileds.sfwtbh"></el-input>
        </el-form-item>
        <el-form-item label="委托方" prop="wtf">
          <el-input v-model="formFileds.wtf"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="鉴定申请人" prop="jdsqr">
          <el-input v-model="formFileds.jdsqr"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="联系电话" prop="jdsqrPhone">
          <el-input v-model="formFileds.jdsqrPhone"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="对方当事人" prop="dfdsr">
          <el-input v-model="formFileds.dfdsr"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="联系电话" prop="dfdsrPhone">
          <el-input v-model="formFileds.dfdsrPhone"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '企业机构'" label="法人(授权代表)" prop="wtfFrSqdb">
          <el-input v-model="formFileds.wtfFrSqdb"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '企业机构'" label="统一社会信用代码" prop="wtfXydm">
          <el-input v-model="formFileds.wtfXydm"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '个人'" label="身份证号码" prop="wtfXydm">
          <el-input v-model="formFileds.wtfXydm" disabled></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx != '司法机构'" label="委托方地址" prop="wtfDz">
          <el-input v-model="formFileds.wtfDz"></el-input>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx != '司法机构'" label="联系方式" prop="wtfLxfs">
          <el-input v-model="formFileds.wtfLxfs"></el-input>
        </el-form-item>

        <el-form-item label="银行卡号" prop="account">
          <el-input v-model="formFileds.account"></el-input>
        </el-form-item>
        <el-form-item label="开户人" prop="accountName">
          <el-input v-model="formFileds.accountName"></el-input>
        </el-form-item>
        <el-form-item label="开户行" prop="bankName">
          <el-input v-model="formFileds.bankName"></el-input>
        </el-form-item>
        <el-form-item label="评估金额" prop="money">
          <el-input type="number" v-model="formFileds.money"></el-input>
          大写：{{ convertCurrency }}
        </el-form-item>

        <el-form-item label="评估目的" prop="pgmd">
          <el-input v-model="formFileds.pgmd"></el-input>
          <!-- <el-select v-model="formFileds.pgmd" multiple collapse-tags>
              <el-option v-for="item in options2" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx == '司法机构'" label="委托日期" prop="wtf">
          <el-date-picker v-model="formFileds.pgjzr" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx != '司法机构'" label="评估基准日" prop="pgjzr">
          <el-input v-model="formFileds.pgjzr"></el-input>
          <!-- <el-date-picker v-model="formFileds.pgjzr" type="date" placeholder="选择日期">
          </el-date-picker> -->
        </el-form-item>
        <el-form-item label="出具报告书时限" prop="cjbgsj">
          <el-input v-model="formFileds.cjbgsj"></el-input>
          <!-- <el-input v-if="this.formFileds.wtflx == '司法机构'" v-model="formFileds.cjbgsj"
              placeholder="正常30天最长60天"></el-input>
          <el-input v-if="this.formFileds.wtflx != '司法机构'" v-model="formFileds.cjbgsj"
              placeholder=" 天"></el-input> -->
        </el-form-item>
        <el-form-item v-if="this.formFileds.wtflx != '司法机构'" label="签约时间" prop="qyrq">
          <el-date-picker v-model="formFileds.qyrq" type="date" value-format="yyyy-MM-ddTHH:mm:ss"
                          placeholder="选择日期" :disabled="selectDate" clearable @change="selectqyrq">
          </el-date-picker>
          <el-tag size="mini" v-if="errortag" class="errortag">请在签约后确认时间</el-tag>
          <el-tag size="mini">以实际签约时间为准，在签约完成之后填写,提交后不可更改</el-tag>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitData()" class="pull-right margin-l-25">确定
          </el-button>
          <el-button @click="dialogClose" class="pull-right">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {setQyrq, update, viewWtht} from "@/api/evaluation/contract";
import {findByName} from '@/api/systemManage/dataDictionary';
import {getCyyByAll} from "@/api/evaluation/word";

export default {
  //接受父组件
  props: {
    editShow: {
      type: Boolean
    },
    record: {
      type: Object
    }
  },

  data() {
    return {
      options: [],
      options1: [],
      options2: [],
      selectDate: false,
      formFileds: {},
      style: {},
      errortag: false,
      rules: {
        wthtlx: [{
          required: true,
          message: '选择委托方类型',
          trigger: 'change,blur'
        }],
        wtf: [{
          required: true,
          message: '请输入委托方',
          trigger: 'change,blur'
        }],
        wtfXydm: [{
          required: true,
          message: '请输入身份证号/统一社会信用代码',
          trigger: 'change,blur'
        }],
        // wtfDz: [{
        //     required: true,
        //     message: '请输入地址',
        //     trigger: 'change,blur'
        // }],
        wtfFrSqdb: [{
          required: true,
          message: '请输入法定代表人/授权代表',
          trigger: 'change,blur'
        }],
        // wtfLxfs: [{
        //     required: true,
        //     message: '请输入联系电话',
        //     trigger: 'change,blur'
        // }],
        pgmd: [{
          required: true,
          message: '请输入评估目的',
          trigger: 'change,blur'
        }],
        pgjzr: [{
          required: true,
          message: '请选择评估基准日',
          trigger: 'change,blur'
        }],
        cjbgsj: [{
          required: true,
          message: '请输入出具报告书时限',
          trigger: 'change,blur'
        }],

      },
      contraction: '',
    }
  },
  methods: {

    dialogClose() {
      this.$emit('dialogClose')
    },
    update(value) {
      if (value == '司法机构') {
        this.rules.wthtlx[0].required = false
      } else {
        this.rules.wthtlx[0].required = true
      }
    },
    selectqyrq() {
      if (this.selectDate) {
        return
      } else {
        if (this.formFileds.qyrq) {
          var nowdate = (new Date()).valueOf()
          var qydate = (new Date(this.formFileds.qyrq)).valueOf()
          if (qydate <= nowdate) {
            this.errortag = false
          } else {
            this.errortag = true
          }
        } else {
          this.errortag = false
        }
      }

    },
    submitData() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          if (this.errortag) {
            return
          } else {
            let pgmdString = ''
            this.formFileds.pgmd.forEach((item, key) => {
              if (key == 0) {
                pgmdString = pgmdString + item
              } else {
                pgmdString = pgmdString + ';' + item
              }
            })
            this.formFileds.pgmd = pgmdString
            this.$delete(this.formFileds, 'url')
            update(this.formFileds).then((res) => {
              this.$emit('getList');
              this.$emit('dialogClose');
            })
          }
        } else {
          return
        }
      });
    },
    dataDictionaryList() {
      findByName('input=' + '委托合同类型').then((res) => {
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].dictName == '委托合同类型')
            this.options1.push({
              label: res.data[i].dictValue,
              value: res.data[i].dictValue
            })
        }
      })
      findByName('input=' + '业务类型').then((res) => {
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].dictName == '业务类型')
            this.options.push({
              label: res.data[i].dictValue,
              value: res.data[i].dictValue
            })
        }
      })
    },
    getpgmd() {
      this.options2 = []
      this.formFileds.pgmd = []
      getCyyByAll('input=' + this.formFileds.ywlx).then((res) => {
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].type == this.formFileds.ywlx)
            this.options2.push({
              label: res.data[i].name,
              value: res.data[i].name,
            });
        }
      })
    },
    pgmd() {
      getCyyByAll('input=' + this.formFileds.ywlx).then((res) => {
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].type == this.formFileds.ywlx)
            this.options2.push({
              label: res.data[i].name,
              value: res.data[i].name,
            });
        }
      })
      if (!this.record.pgmd) {
        this.formFileds.pgmd = []
      } else {
        this.formFileds.pgmd = this.record.pgmd.split(';')
      }
    },
    // contractionView () {
    //     this.contraction = 'http://www.jianpingyun.com/api/wtht/createWtht3?wthtid=' + this.record.id
    // },
    // contractiondown () {
    //     this.contraction = 'http://www.jianpingyun.com/api/wtht/createWtht2?wthtid=' + this.record.id
    // }

  },
  mounted() {
    this.dataDictionaryList();
    this.formFileds = Object.assign({}, this.record)
    this.pgmd()
    if (this.formFileds.qyrq) {
      this.selectDate = true
    }
  },
  computed:{
    convertCurrency() {
      let money = this.formFileds.money;
      //汉字的数字
      var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
      //基本单位
      var cnIntRadice = new Array('', '拾', '佰', '仟');
      //对应整数部分扩展单位
      var cnIntUnits = new Array('', '万', '亿', '兆');
      //对应小数部分单位
      var cnDecUnits = new Array('角', '分', '毫', '厘');
      //整数金额时后面跟的字符
      var cnInteger = '整';
      //整型完以后的单位
      var cnIntLast = '元';
      //最大处理的数字
      var maxNum = 999999999999999.9999;
      //金额整数部分
      var integerNum;
      //金额小数部分
      var decimalNum;
      //输出的中文金额字符串
      var chineseStr = '';
      //分离金额后用的数组，预定义
      var parts;
      // 传入的参数为空情况
      if (money == '') {
        return '';
      }
      money = parseFloat(money)
      if (money >= maxNum) {
        return ''
      }
      // 传入的参数为0情况
      if (money == 0) {
        chineseStr = cnNums[0] + cnIntLast + cnInteger;
        return chineseStr
      }
      // 转为字符串
      money = money.toString();
      // indexOf 检测某字符在字符串中首次出现的位置 返回索引值（从0 开始） -1 代表无
      if (money.indexOf('.') == -1) {
        integerNum = money;
        decimalNum = ''
      } else {
        parts = money.split('.');
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 4);
      }
      //转换整数部分
      if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0;
        let IntLen = integerNum.length
        for (let i = 0; i < IntLen; i++) {
          let n = integerNum.substr(i, 1);
          let p = IntLen - i - 1;
          let q = p / 4;
          let m = p % 4;
          if (n == '0') {
            zeroCount++;
          } else {
            if (zeroCount > 0) {
              chineseStr += cnNums[0]
            }
            zeroCount = 0;
            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if (m == 0 && zeroCount < 4) {
            chineseStr += cnIntUnits[q];
          }
        }
        // 最后+ 元
        chineseStr += cnIntLast;
      }
      // 转换小数部分
      if (decimalNum != '') {
        let decLen = decimalNum.length;
        for (let i = 0; i < decLen; i++) {
          let n = decimalNum.substr(i, 1);
          if (n != '0') {
            chineseStr += cnNums[Number(n)] + cnDecUnits[i]
          }
        }
      }
      if (chineseStr == '') {
        chineseStr += cnNums[0] + cnIntLast + cnInteger;
      } else if (decimalNum == '') {
        chineseStr += cnInteger;
      }
      this.formFileds.moneyCn = chineseStr;
      return chineseStr;
    }
  }

}
</script>

<style lang="less" scoped>
:deep .el-dialog__body {
  padding: 30px 80px 30px 40px;
}

.errortag {
  background-color: #fff;
  color: red;
  border: none;
}

.buttdiv {
  width: 100%;
  margin: 0px 0px 40px 0px;
}

.butt {
  text-decoration: underline;
  float: right;
}

a {
  text-decoration: underline;
  float: right;
  margin-left: 20px;
}
</style>
